//
//  OriginalView.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/19.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

//MARK: - 属性，构造函数，生命周期函数
class OriginalView: UIView {
    
    var viewModel: StatusViewModel? {
        
        didSet {
            
            //设置真实的微博数据， view引用viewModle，viewModel引用model
            avatarIcon.setCircleImage(viewModel?.status.user?.profile_image_url, placeholder: "avatar_default_big", isCornered: true, size: cellLayout.userAvatarSize)
            
            screenName.text = viewModel?.status.user?.screen_name
            levelIcon.image = viewModel?.levelIconImage
            vipIcon.image = viewModel?.vipIconImage
            timeLabel.text = viewModel?.timeText
            sourceLabel.text = viewModel?.sourceText
            statusLabel.text = viewModel?.status.text
            
            pictureView.pic_urls = viewModel?.status.pic_urls
            pictureView.viewModel = viewModel
            
            var pictureOffset: CGFloat = 0
            
            if let pic_urls = viewModel?.status.pic_urls where pic_urls.count > 0 {
                
                pictureOffset = cellLayout.seprator
            }
            
            pictureView.snp_updateConstraints { (make) -> Void in
                
                make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout.seprator)
                make.left.equalTo(statusLabel)
                make.size.equalTo((viewModel?.pictureViewSize)!)
                make.bottom.equalTo(self).offset(-pictureOffset)
            }
        }
    }
    
    let seprator = UIView()

    /// 头像
    let avatarIcon = UIImageView()
    
    /// 昵称
    let screenName = UILabel(text: "", fontSize: 15)
    
    /// 👑
    let levelIcon = UIImageView()
    
    /// VIP
    let vipIcon = UIImageView()
    
    /// 发布时间
    let timeLabel = UILabel(text: "", textColor: UIColor(white: 0.4, alpha: 1.0), fontSize: 11)
    
    /// 来源
    let sourceLabel = UILabel(text: "", textColor: UIColor(white: 0.4, alpha: 1.0), fontSize: 11)
    
    /// 微博文字
    let statusLabel = UILabel(text: "", fontSize: 15)
    
    let pictureView = PictureView()
    
    override init(frame: CGRect) {
        
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 设置UI
extension OriginalView {
    
    func setupUI() {
        
        //添加子控件
        addSubview(avatarIcon)
        addSubview(screenName)
        addSubview(levelIcon)
        addSubview(vipIcon)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(statusLabel)
        addSubview(seprator)
        addSubview(pictureView)
        
        backgroundColor = UIColor.whiteColor()
        
        seprator.backgroundColor = UIColor.brownColor()
        
        seprator.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.height.equalTo(2)
        }
        
        //自动布局
        avatarIcon.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(cellLayout.seprator)
            make.top.equalTo(seprator).offset(cellLayout.seprator)
            make.size.equalTo(cellLayout.userAvatarSize)
        }
        
        screenName.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(avatarIcon.snp_right).offset(cellLayout.seprator)
            make.top.equalTo(avatarIcon)
        }
        
        levelIcon.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(screenName.snp_right).offset(3)
            make.top.equalTo(screenName).offset(3)
        }
        
        vipIcon.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(avatarIcon.snp_right).offset(-5)
            make.bottom.equalTo(avatarIcon).offset(5)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(avatarIcon.snp_right).offset(cellLayout.seprator)
            make.bottom.equalTo(avatarIcon)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(cellLayout.seprator)
            make.bottom.equalTo(avatarIcon)
        }
        
        statusLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(avatarIcon)
            make.right.equalTo(self).offset(-cellLayout.seprator)
            make.top.equalTo(avatarIcon.snp_bottom).offset(cellLayout.seprator)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout.seprator)
            make.left.equalTo(statusLabel)
            make.size.equalTo(CGSizeZero)
            make.bottom.equalTo(self).offset(-cellLayout.seprator)
        }
    }
}
